﻿/*===================================================
* CLR版本  : 4.0.30319.42000 
* 类名称   : UI
* 文 件 名 : UI 
* 类描述   :
* 创建人   : oream
* 创建时间 : 2018/2/13 20:57:56
* 修改人   : 
* 修改时间 :
* 版本： @version 1.0
=====================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Hotfix
{
  [ObjectSystem]
  public class UISystem : ObjectSystem<UI>, IAwake<Scene, UI, GameObject>
  {
    public void Awake(Scene t1, UI t2, GameObject t3)
    {
      this.Get().Awake(t1, t2, t3);
    }
  }

  public class UI : Entity
  {
    public Scene Scene { get; private set; }
    public GameObject GameObject { get; set; }

    public Dictionary<string, UI> dicChidren = new Dictionary<string, UI>();

    public string Name
    {
      get
      {
        return this.GameObject.name;
      }
    }
    public void Awake(Scene t1, UI t2, GameObject t3)
    {
      Debug.Log("加载了entity");
    }

    public void Add(UI ui)
    {
      this.dicChidren.Add(ui.Name, ui);
      ui.Parent = this;
    }


    public override void Dispose()
    {
      if (this.id == 0)
      {
        return;
      }
      base.Dispose();
      foreach (var item in dicChidren.Values)
      {
        item.Dispose();
      }
      UnityEngine.GameObject.Destroy(this.GameObject);
      dicChidren.Clear();
    }

    public void SetAsFirstSibling()
    {
      this.GameObject.transform.SetAsFirstSibling();
    }

    public void Romove(string name)
    {
      if (this.dicChidren.ContainsKey(name))
      {
        this.dicChidren[name].Romove(name);
        this.dicChidren.Remove(name);
      }
    }

    public UI Get(string name)
    {
      UI child;
      if (this.dicChidren.TryGetValue(name, out child))
      {
        return child;
      }
      GameObject childGameObject = this.GameObject.transform.Find(name)?.gameObject;
      if (childGameObject == null) return null;

      child = EntityFactory.Create<UI, Scene, UI, GameObject>(this.Scene, this, childGameObject);
      this.Add(child);
      return child;
    }

  }
}
